package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.House;
import com.atguigu.entity.HouseImage;
import com.atguigu.result.Result;
import com.atguigu.service.HouseImageService;
import com.atguigu.service.HouseService;
import com.atguigu.util.QiniuUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/houseImage")
public class HouseImageController extends BaseController {
    private static final String ACTION_LIST = "redirect:/house/detail/";
    private static final String PAGE_UPLOAD_SHOW = "house/upload";
    private static final String PAGE_UPLOAD_SHOW1 = "house/upload1";

    @Reference
    HouseImageService houseImageService;

    @Reference
    HouseService HouseService;

    @RequestMapping("/delete/{houseId}/{id}")
    public String upload(@PathVariable("houseId") Long houseId,
                         @PathVariable("id") Long id) {
        HouseImage houseImage = houseImageService.getById(id);
        String imageName = houseImage.getImageName();

        houseImageService.delete(id);

        QiniuUtils.deleteFileFromQiniu(imageName);

        return ACTION_LIST + houseId;
    }



    @RequestMapping("/uploadShow1/{houseId}/{type}")
    public String uploadShow1(@PathVariable("houseId") Long houseId, @PathVariable("type") Integer type, Map map){

        map.put("houseId", houseId);
        map.put("type", type);
        return PAGE_UPLOAD_SHOW1;
    }

    @RequestMapping("/upload1/{houseId}/{type}")
    public String upload1(@PathVariable("houseId") Long houseId, @PathVariable("type") Integer type, @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        String newFileName =  UUID.randomUUID().toString() ;
        // 上传图片
        QiniuUtils.upload2Qiniu(file.getBytes(),newFileName);
        String url= "http://rdv1ostyt.hn-bkt.clouddn.com/"+ newFileName;
        House house = HouseService.getById(houseId);
        house.setDefaultImageUrl(url);
        HouseService.update(house);

        HouseImage houseImage = new HouseImage();
        houseImage.setHouseId(houseId);
        houseImage.setImageName(newFileName);
        houseImage.setImageUrl(url);
        houseImage.setType(1);
        houseImageService.insert(houseImage);
        return this.successPage(this.MESSAGE_SUCCESS, request);
    }



    @RequestMapping("/upload/{houseId}/{type}")
    @ResponseBody
    //<input type="fiLe" name=""file" multiple="multiple"">支持上传多个文件，name的名称"file”固定的
    public Result upload(@PathVariable("houseId") Long houseId, @PathVariable("type") Integer type
            ,@RequestParam("file") MultipartFile[] files) throws IOException {
        if (files != null && files.length > 0) {
            for (MultipartFile file : files) {
                byte[] bytes = file.getBytes();
//                String originalFilename = file.getOriginalFilename();//原始文件名称
//                QiniuUtils.upload2Qiniu(bytes,originalFilename);//不能使用原始文件名称进行上传，否则，后上传的文件可能覆盖我们先上传的文件
                String newFileNAme = UUID.randomUUID().toString();
                //图片上传到七牛云
                QiniuUtils.upload2Qiniu(bytes,newFileNAme);
                String imageUrl = "http://rdv1ostyt.hn-bkt.clouddn.com/" + newFileNAme;
                //将图片路径信息等保存到数据库
                HouseImage houseImage = new HouseImage();
                houseImage.setHouseId(houseId);
                houseImage.setImageName(newFileNAme);
                houseImage.setImageUrl(imageUrl);
                houseImage.setType(type);
                houseImageService.insert(houseImage);

            }
        }
        return Result.ok();
    }




    @RequestMapping("/uploadShow/{houseId}/{type}")
    public String uploadShow(@PathVariable("houseId") Long houseId, @PathVariable("type") Integer type, Map map){

        map.put("houseId", houseId);
        map.put("type", type);
        return PAGE_UPLOAD_SHOW;
    }
}
